package gui;

import gui.profesor.DialogoProfesor;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import manejador.ManejadorDeEmpleado;
import utils.Utilidad;
import javax.swing.ImageIcon;

public class Login extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JTextField txtUsuario = null;
	private JLabel jLabel1 = null;
	private JTextField txtClave = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private Utilidad validadora = null;
	private static int idProfesor = 0;
	private JPanel panelIniciarSesion = null;

	/**
	 * This is the default constructor
	 */
	public Login() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(416, 251);
		this.setContentPane(getJContentPane());
		this.setTitle("Iniciar Sesion");
		validadora = Utilidad.getInstancia();
		this.setLocationRelativeTo(null);

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Clave:");
			jLabel1.setBounds(new Rectangle(51, 92, 59, 28));
			jLabel = new JLabel();
			jLabel.setText("Usuario:");
			jLabel.setFont(new Font("Dialog", Font.BOLD, 12));
			jLabel.setBounds(new Rectangle(51, 47, 59, 28));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanelIniciarSesion(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtUsuario() {
		if (txtUsuario == null) {
			txtUsuario = new JTextField();
			txtUsuario.setBounds(new Rectangle(138, 47, 185, 28));
			txtUsuario.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e) {
					int key = e.getKeyCode();
					if (key == KeyEvent.VK_ENTER) {
						iniciarSesion();
					}
				}
			});
		}
		return txtUsuario;
	}

	/**
	 * This method initializes jTextField1
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtClave() {
		if (txtClave == null) {
			txtClave = new JPasswordField();
			txtClave.setBounds(new Rectangle(138, 92, 185, 28));
			txtClave.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e) {
					int key = e.getKeyCode();
					if (key == KeyEvent.VK_ENTER) {
						iniciarSesion();
					}
				}
			});
		}
		return txtClave;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Iniciar Sesion");
			jButton.setIcon(new ImageIcon("./lib/images/login.png"));
			jButton.setBounds(new Rectangle(41, 145, 133, 26));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					iniciarSesion();
				}
			});
		}
		return jButton;
	}

	public void iniciarSesion() {
		if (validadora.validar(txtUsuario.getText(), txtClave.getText()) == false) {
			JOptionPane.showMessageDialog(null, "Faltan Campos por llenar");
		} else {
			int idRol = ManejadorDeEmpleado.getInstancia().validarLogin(
					txtUsuario.getText(), txtClave.getText());
			if (idRol == 0) {
				JOptionPane
						.showMessageDialog(null, "Usuario o Clave no existe");

			} else {

				setIdProfesor(ManejadorDeEmpleado.getInstancia().getIdEmpleado(
						txtUsuario.getText()));
				if (idRol == 1) {

					validadora.limpiar(txtUsuario, txtClave);
					this.setVisible(false);
					VentanaPrincipal ventana = new VentanaPrincipal();
					ventana.setVisible(true);

				} else if (idRol == 2) {
					this.setVisible(false);
					validadora.limpiar(txtUsuario, txtClave);
					DialogoProfesor.getInstancia().setVisible(true);
				} else if (idRol == 3) {

				}
			}
		}
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Salir");
			jButton1.setBounds(new Rectangle(196, 145, 127, 26));
			jButton1.setIcon(new ImageIcon("./lib/images/exit.png"));
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return jButton1;
	}

	public static int getIdProfesor() {
		return idProfesor;
	}

	public static void setIdProfesor(int idProfesor) {
		Login.idProfesor = idProfesor;
	}

	/**
	 * This method initializes panelIniciarSesion
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelIniciarSesion() {
		if (panelIniciarSesion == null) {
			panelIniciarSesion = new JPanel();
			panelIniciarSesion.setLayout(null);
			panelIniciarSesion.setBounds(new Rectangle(12, 10, 376, 197));
			panelIniciarSesion.setBorder(BorderFactory.createTitledBorder(
					BorderFactory.createLineBorder(Color.gray, 2),
					"Iniciar Sesion", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog",
							Font.BOLD, 12), new Color(51, 51, 51)));
			panelIniciarSesion.add(jLabel, null);
			panelIniciarSesion.add(jLabel1, null);
			panelIniciarSesion.add(getJButton(), null);
			panelIniciarSesion.add(getTxtUsuario(), null);
			panelIniciarSesion.add(getTxtClave(), null);
			panelIniciarSesion.add(getJButton1(), null);
		}
		return panelIniciarSesion;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
